package com.gitee.cashzhang27.test.cloud.oauth.auth.server.extended.token;

import com.gitee.cashzhang27.test.cloud.oauth.auth.server.extended.entity.DictCodeEnums;
import java.util.Collection;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/02/21 11:46
 */
public class EmailNumberVerifyCodeAuthenticationToken extends AbstractAuthenticationToken {

  private static final long serialVersionUID = -5736720708234155574L;

  private final Object principal;
  private Object credentials;
  private DictCodeEnums userType;

  public EmailNumberVerifyCodeAuthenticationToken(Object principal, Object credentials) {
    super(null);
    this.principal = principal;
    this.credentials = credentials;
    setAuthenticated(false);
  }

  public EmailNumberVerifyCodeAuthenticationToken(Object principal, Object credentials,
      DictCodeEnums userType) {
    super(null);
    this.principal = principal;
    this.credentials = credentials;
    this.userType = userType;
    setAuthenticated(false);
  }


  public EmailNumberVerifyCodeAuthenticationToken(Object principal, Object credentials,
      Collection<? extends GrantedAuthority> authorities) {
    super(authorities);
    this.principal = principal;
    this.credentials = credentials;
    // must use super, as we override
    super.setAuthenticated(true);
  }

  public EmailNumberVerifyCodeAuthenticationToken(Object principal, Object credentials,
      DictCodeEnums userType,
      Collection<? extends GrantedAuthority> authorities) {
    super(authorities);
    this.principal = principal;
    this.credentials = credentials;
    this.userType = userType;
    // must use super, as we override
    super.setAuthenticated(true);
  }

  @Override
  public Object getCredentials() {
    return this.credentials;
  }

  @Override
  public Object getPrincipal() {
    return this.principal;
  }

  public DictCodeEnums getUserType() {
    return this.userType;
  }

  @Override
  public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
    if (isAuthenticated) {
      throw new IllegalArgumentException(
          "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
    }

    super.setAuthenticated(false);
  }

  @Override
  public void eraseCredentials() {
    super.eraseCredentials();
    credentials = null;
  }
}
